%% Fig S22A
load('Diode\C0020.mat')

idx = [1, 348, 1350, 2347, 3346, 4348, 5350, 6347, 7347, 8347, 9348, 10348];
figure(3); clf;
colors = [1,0,0; 0.5,0.5,0.5];
k = 1;
for i = 1:2:10
    
    plot(datatest(idx(i):idx(i+1), 1) / 249 * 1e5, datatest(idx(i):idx(i+1), 2) / 200 * 1000, 'color', colors(k, :));
    hold all;
%     plot(datatest(idx(i+1):idx(i+2), 1), datatest(idx(i+1):idx(i+2), 2), '--', 'color', colors(k, :));

    %plot using grey on subsequent sweeps
    k = 2;
end

pause(1);
i = 1
plot(datatest(idx(i):idx(i+1), 1) / 249 * 1e5, datatest(idx(i):idx(i+1), 2) / 200 * 1000, 'color', colors(1, :));
hold all;

xlabel('Current / Area (A / cm^2)')
ylabel('Voltage (mV)')
legend('First cycle', 'Subsequent Cycles')


%% I Series: first curve
load('Diode\I0020.mat')


idx = [1, 1568, 3567, 5569, 7571, 9565, 11572];
figure(3); clf;
colors = [1,0,0; 0.5,0.5,0.5];
k = 1;
for i = 1:2:4
    
    plot(datatest(idx(i):idx(i+1), 1) / 249 * 1e5, datatest(idx(i):idx(i+1), 2) / 200 * 1000, 'color', colors(k, :));
    hold all;
%     plot(datatest(idx(i+1):idx(i+2), 1), datatest(idx(i+1):idx(i+2), 2), '--', 'color', colors(k, :));

    %plot using grey on subsequent sweeps
    k = 2;
    pause(1);
end

xlabel('Current (A / cm^2)')
ylabel('Voltage (mV)')
legend('First cycle', 'Subsequent Cycles')



%%
figure(1); clf;
load('Diode\J00800.mat');
yyaxis left;
plot(datatest(:, 1)); hold all;
yyaxis right;
plot(datatest(:, 2));

yyaxis left;

derivative = diff(datatest(:, 1));
turnpt = find((datatest(:, 1) < -1.67) .* ([0;0; derivative(1:end-1) .* derivative(2:end) < 0]));
%turnpt = [0;0; derivative(1:end-1) .* derivative(2:end) <= 0];

turnptPos = find((datatest(:, 1) > 1.67) .* ([0;0; derivative(1:end-1) .* derivative(2:end) < 0]));

%find the critical currents on the down sweeps.
IcList = zeros(1, length(turnptPos));
for i = 1:length(turnpt)
    idx = turnptPos(i) : turnpt(i);
    [~, idx0] = min(abs(datatest(idx, 1)));
    idx0 = idx0 + idx(1);

    segment = idx0:turnpt(i);
    idxIc = find(datatest(segment, 2) < -2.7, 1);
    idxIc = segment(1) + idxIc - 1;
    IcList(i) = datatest(idxIc, 1);

    plot(idxIc, IcList(i), 'o', 'color', [1,0,0]);
end

% plot the IV curve.
figure(11); clf;
k = 158;
plot(datatest(turnptPos(k):turnpt(k), 1) / 249 * 1e5, datatest(turnptPos(k):turnpt(k), 2) / 200 * 1000, 'color', [0,0,0]);
hold all;
%
figure(1); clf;
load('Diode\L0010.mat');
yyaxis left;
plot(datatest(:, 1)); hold all;
yyaxis right;
plot(datatest(:, 2));

yyaxis left;

%find turning points.
idx0 = find((sign(datatest(1:end-1, 1)) .* sign(datatest(2:end, 1))) < 0);
k = 1;
for i = 1:2:(length(idx0)-2)
    [~, turnptPos(k)] = max(datatest(idx0(i) : idx0(i+2)));
    turnptPos(k) = turnptPos(k) + idx0(i) - 1;

    [~, turnpt(k)] = min(datatest(idx0(i) : idx0(i+2)));
    turnpt(k) = turnpt(k) + idx0(i) - 1;
    k = k+1; 
end


plot(turnptPos, 1.5, 'o')
plot(turnpt, -1.5, 'o')

%find the critical currents on the down sweeps.
IcList2 = zeros(1, length(turnptPos));
for i = 1:length(turnpt)
    idx = turnptPos(i) : turnpt(i);
    [~, idx0] = min(abs(datatest(idx, 1)));
    idx0 = idx0 + idx(1);

    segment = idx0:turnpt(i);
    idxIc = find(datatest(segment, 2) < -2.7, 1);
    idxIc = segment(1) + idxIc - 1;
    IcList2(i) = datatest(idxIc, 1);

    plot(idxIc, IcList2(i), 'o', 'color', [1,0,0]);
end

figure(11);
k = 155;
plot(datatest(turnptPos(k):turnpt(k), 1) / 249 * 1e5, datatest(turnptPos(k):turnpt(k), 2) / 200 * 1000, 'color', [1,0,0]);
hold all;

xlabel('Current (arb)');
ylabel('Voltage (arb)');

figure(3); clf;
n = 45;
histogram(IcList / 249 * 1e5, n);
hold all;
histogram(IcList2 / 249 * 1e5, n);

xlim([-600, -0.0]);

xlabel('Current (arb)');
ylabel('N')